import httplib, mimetypes
from core import opener
import urllib2

def post_multipart(host, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
##    h = httplib.HTTP(host)
##    h.putrequest('POST', selector)
##    h.putheader('content-type', content_type)
##    h.putheader('content-length', str(len(body)))
##    h.putheader('User-Agent','Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)')
##    h.putheader('Referer','http://www.firstdrs.com/admin/first/quicksend.do?id=4070136')
##    h.putheader('Connection','Keep-Alive')
##    h.endheaders()
##    h.send(body)
##    errcode, errmsg, headers = h.getreply()
##    return h.file.read()
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1',
               'Host':'firstdrs.com',
               'Content-Type': content_type,
               'Content-Length': str(len(body)),
               'Connection':'keep-alive',
               'Accept-Encoding':'gzip,deflate,sdch',
               'Cache-Control': 'max-age=0',
               'Origin': 'http://firstdrs.com',
               'Referer':'http://firstdrs.com/admin/first/quicksend.do?id=4072299',
               }
    r = urllib2.Request("http://%s%s" % (host, selector), body, headers)
##    headers = [("User-Agent","Mozilla/4.0 (compatible; MSIE 7.1; Windows NT 5.1; SV1)"),
##               ("Content-Type", content_type),
##               ("Content-Length",str(len(body))),
##               ]
##    opener.addheaders = headers
##    req = urllib2.Request("http://%s%s" % (host, selector))
##    r = opener.open(req,body)
##    #return 0
    return urllib2.urlopen(r).read()
 
def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body
 
def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
